package com.pzl.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pzl.entity.PageResult;
import com.pzl.mapper.BrandMapper;
import com.pzl.pojo.goods.Brand;
import com.pzl.service.goods.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * BrandServiceImpl class
 *
 * @Author panzhenglei
 * @Date 2019/6/21 19:46
 */

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }

    @Override
    public PageResult<Brand> fingPage(int page, int size) {
       PageHelper.startPage(page, size);
        Page<Brand> brands = (Page<Brand>)brandMapper.selectAll();
        return new PageResult<>(brands.getTotal(),brands.getResult());
    }

    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = findByExample(searchMap);
        return brandMapper.selectByExample(example);
    }

    @Override
    public PageResult<Brand> fingPage(int page, int size, Map<String, Object> searchMap) {
        Example example = findByExample(searchMap);
        PageHelper.startPage(page,size);
        Page<Brand> brands = (Page<Brand>) brandMapper.selectByExample(example);
        return new PageResult<>(brands.getTotal(),brands.getResult());
    }

    @Override
    public Brand findById(int id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 封装根据条件查询
     * @param searchMap
     * @return 返回条件封装的example对象
     */
    private Example findByExample(Map<String, Object> searchMap){
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        if (searchMap!=null){
            /*
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name")) ){
                criteria.andLike("name","%"+(String)searchMap.get("name")+"%");
            }
            */
            System.out.println(searchMap.get("name"));
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+(String) searchMap.get("name")+"%");
            }
            if (searchMap.get("letter")!=null&&!"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",(String)searchMap.get("letter"));
            }
        }
        return example;
    }


}
